package cn.org.donald.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.org.donald.framework.service.SysRoleService;
import cn.org.donald.framework.constant.TableConstant;
import cn.org.donald.framework.pojo.dto.AddUserRoleDto;
import cn.org.donald.framework.pojo.dto.PageDto;
import cn.org.donald.mapper.RoleMapper;
import cn.org.donald.mapper.UserRoleMapper;
import cn.org.donald.pojo.entity.Role;
import cn.org.donald.pojo.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ： Donald
 * @date ： 2020/10/18 00:25
 * @description：
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements SysRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;


    @Override
    public IPage<Role> queryListPage(PageDto<Role> pageDto) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>(pageDto.getQuery());
        return roleMapper.selectPage(pageDto, queryWrapper);
    }

    @Override
    public boolean isAdmin(Long id) {
        Role role = roleMapper.selectById(id);
        return role.getRoleName().equals(TableConstant.VALUE_ADMIN_NAME);
    }

    @Override
    @Transactional
    public int deleteById(Long id) {
        return roleMapper.deleteById(id);
    }


    /**
     * 查询用户没有分配的角色
     * @param id
     * @return
     */
    @Override
    public List<Role> findByNotUserId(Long id) {
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id",id);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);
        List<Long> longs = new ArrayList<>();
        userRoles.forEach(userRole -> longs.add(userRole.getRoleId()));
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.notIn("id",longs);
        return roleMapper.selectList(queryWrapper);
    }

    @Override
    public int insertUserRoleBatch(AddUserRoleDto addUserRoleDto) {
        List<Role> listDto = addUserRoleDto.getRoleList();

        return listDto.stream()
                .mapToInt(role -> {
                    if (role.getId()!=null){
                        UserRole userRole = new UserRole();
                        userRole.setUserId(addUserRoleDto.getId());
                        userRole.setRoleId(role.getId());
                        return userRoleMapper.insert(userRole);
                    }
                    return 0;
                }).sum();
    }

    @Override
    public int deleteUserRole(UserRole userRole) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>(userRole);
        return userRoleMapper.delete(queryWrapper);
    }

    /**
     * 根据userId查询角色
     * @param userId
     * @return
     */
    @Override
    public List<Role> findByUserId(Long userId) {

        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id",userId);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);

        List<Role> roles = new ArrayList<>();
        userRoles.forEach(userRole -> {
            Role role = roleMapper.selectById(userRole.getRoleId());
            roles.add(role);
        });

       return roles;
    }


}
